package com.zhoujie.broadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.DownloadManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        findViewById(R.id.button_notification).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.notify(0, getNotification());
            }
        });
        findViewById(R.id.button_download).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                download();
            }
        });
        //需要注册ACTION_NOTIFICATION_CLICKED
        registerReceiver(mDownloadClickReceiver, new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
        //注册下载完成广播
        registerReceiver(mDownloadCompletedReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    private BroadcastReceiver mDownloadCompletedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show();
        }
    };

    private BroadcastReceiver mDownloadClickReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "应用正在更新", Toast.LENGTH_SHORT).show();
        }
    };


    private Notification getNotification() {
        String chanelId = "update";
        CharSequence channelName = "update";//渠道名称
        String description = "一条简单的测试通知";
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, chanelId);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("测试通知")
                .setContentText("周杰有点帅")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);//设置渠道重要性，8.0以上必须设置
        //创建渠道，8.0以上必须创建
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(chanelId, channelName, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
        // Create an explicit intent for an Activity in your app
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        return builder.build();
    }

    //使用downloadManger进行下载
    private void download() {
        DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        String apkUrl = "https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk";
        Uri resource = Uri.parse(apkUrl);
        DownloadManager.Request request = new DownloadManager.Request(resource);
        request.setTitle("下载测试文件");
        String fileName = apkUrl.substring(apkUrl.lastIndexOf("/"));
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
        if (downloadManager != null) {
            downloadManager.enqueue(request);
        }
    }
}
